//
//  UCMeasureMarker.m
//  SDK开发版本
//
//  Created by 北京国泰北斗科技有限公司 on 2017/3/7.
//  Copyright © 2017年 北京国泰北斗科技有限公司. All rights reserved.
//

#import "UCMeasureMarker.h"

@interface UCMeasureMarker()

@property(nonatomic,copy)NSString *contentStr;
@property(nonatomic,strong)NSDictionary *config;

@end

@implementation UCMeasureMarker

+ (instancetype)measureMarkerWithLocation1:(CLLocationCoordinate2D)coord1
                                 location2:(CLLocationCoordinate2D)coord2
                                    string:(NSString *)dataString{
    
    CLLocationDegrees latitude = fabs((coord1.latitude - coord2.latitude)/2 + coord2.latitude);
    CLLocationDegrees longitude = fabs((coord1.longitude - coord2.longitude)/2 + coord2.longitude);
    
    CLLocationCoordinate2D centerLocation = CLLocationCoordinate2DMake(latitude, longitude);
    
    UCMeasureMarker *marker = [UCMeasureMarker markerWithPosition:centerLocation];
    marker.contentStr = dataString;
    marker.draggable = NO;
    [marker iconForText:dataString];
    marker.groundAnchor = CGPointMake(0.5, 0.5);
    return marker;
}

+ (instancetype)measureMarkerCenterLocation:(CLLocationCoordinate2D)center string:(NSString *)dataString{
    
    UCMeasureMarker *marker = [UCMeasureMarker markerWithPosition:center];
    marker.contentStr = dataString;
    marker.draggable = NO;
    [marker iconForText:dataString];
    marker.groundAnchor = CGPointMake(0.5, 0.5);
    return marker;
}

- (void)changeLocationWithLocation1:(CLLocationCoordinate2D)coord1
                          location2:(CLLocationCoordinate2D)coord2
                             string:(NSString *)dataString{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        CLLocationDegrees latitude = fabs((coord1.latitude - coord2.latitude)/2 + coord2.latitude);
        CLLocationDegrees longitude = fabs((coord1.longitude - coord2.longitude)/2 + coord2.longitude);
        CLLocationCoordinate2D centerLocation = CLLocationCoordinate2DMake(latitude, longitude);
        dispatch_async(dispatch_get_main_queue(), ^{
            _contentStr = dataString;
            self.position = centerLocation;
            [self iconForText:dataString];
        });
    });
}

- (void)iconForText:(NSString *)text{
    _contentStr = text;
    if (!_config) {
        _config = @{
                    NSFontAttributeName : [UIFont systemFontOfSize:13],
                    NSForegroundColorAttributeName : [UIColor whiteColor],
                    NSBackgroundColorAttributeName : [UIColor blackColor]
                    };
    }
    [self private_setIconImage:_config];
}

- (void)setContentLabelCongfig:(NSDictionary<NSString *,id> *)configDic{
    
    _config = configDic;
    [self private_setIconImage:configDic];
}

- (void)private_setIconImage:(NSDictionary *)dic{

    CGSize size = [_contentStr sizeWithAttributes:dic];
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
    
    //2、设置裁剪区域
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3];
    [path addClip];
    
    [_contentStr drawInRect:rect withAttributes:dic];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    self.icon = newImage;
}

@end
